package rulusoft.nook.rssReader;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class xmlListHandler extends DefaultHandler {
	/************ VARIABLES **************/	
	String currentValue=null;
	boolean inSubscriptionList=false;
	boolean inFeedId = false;
	boolean inFeedTitle = false;
	boolean currentElement=false;
	public static subscriptions subs=new subscriptions();
	
	public static subscriptions getSubscriptionsList() {
		return subs;
	}
	public static void setSubscriptionsList(subscriptions subs) {
		xmlListHandler.subs = subs;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		 if(localName.equals("list") 
				&& attributes.getValue("name").equals("subscriptions")) {
			inSubscriptionList=true;
			/********START**********/
		}
		else if(inSubscriptionList) {
			if(localName.equals("object")) {
			}
			 if(localName.equals("string")
					&& attributes.getValue("name").equals("id")) {
				 currentElement = true;
				 inFeedId = true;
			}
			else if(localName.equals("string")
				&& attributes.getValue("name").equals("title")) {
				currentElement = true;
				inFeedTitle = true;
			}
			else {
				; //do nothing
			}
		}
		
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		currentElement = false;
		
		if(inSubscriptionList) {
			if(inFeedId) {
				if(localName.equalsIgnoreCase("string")) {
					subs.setId(currentValue);
					inFeedId = false;
				}
			}
			else if(inFeedTitle) {
				if(localName.equalsIgnoreCase("string")) {
					subs.setTitle(currentValue);
					inFeedTitle = false;
				}
			}
		}
		else if(localName.equals("list")) {
			inSubscriptionList = false;
		}
		else {
			; //do nothing
		}
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
		
	}

}
